package com.zgxl.common.es;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.net.InetAddress;

@Component
public class ElasticConnUtil {
    private static final Logger log = LoggerFactory.getLogger(ElasticConnUtil.class);

    private TransportClient transportClient = null;

    @Autowired
    private ElasticSearchConf elasticSearchConf;

    /**
     * 全文检索链接
     */
    public TransportClient getTransportClient() throws Exception {
        if (transportClient == null) {
            Settings settings = Settings.builder().put("cluster.name", elasticSearchConf.getClusterName()).build();
            transportClient = new PreBuiltTransportClient(settings).addTransportAddress(new org.elasticsearch.common.transport.TransportAddress(InetAddress.getByName(elasticSearchConf.getHost()), elasticSearchConf.getPort()));
        }
        return transportClient;
    }

}